package com.augurit.gzcgwsp.state.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.state.entity.UserStateInfo;
import com.augurit.gzcgwsp.state.service.ISummaryService;
import com.augurit.gzcgwsp.state.util.RequestUtil;
import com.augurit.gzcgwsp.state.web.form.SummaryStateForm;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("summaryStateAtcion")
@ParentPackage("struts-default")
@Namespace("/state")
@Scope("prototype")
@Action(value="summary-state",results={
	 	@Result(name = "success", location = "/gzcgwsp/state/listSummaryState.jsp"),
	 	@Result(name = "toAreaState", location = "/gzcgwsp/state/listAreaState.jsp"),
	 	@Result(name = "resultState", location = "/gzcgwsp/state/rqstate/listResultState.jsp"),
	 	@Result(name = "resultInfo", location = "/gzcgwsp/state/listSummaryData.jsp"),
	 	@Result(name = "userInfo", location = "/gzcgwsp/state/listUserSummaryData.jsp"),
		@Result(name = "areaData", location = "/gzcgwsp/state/listareaData.jsp"),
	 	@Result(name = "userStateInfo", location = "/gzcgwsp/state/userStateInfo.jsp")
	 	
	}
)
/**
 * 行政许可审批查询Action
 */
public class SummaryStateAtcion extends ActionSupport implements ModelDriven {
	@Autowired
	private ISummaryService summaryService;
	private Page page ;

	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	
	/**
	 * 设置用户的自定报表
	 * @return 
	 * @date 2012-4-22
	 */
	public String getUserStateInfo(){
		LoginUserForm userform=(LoginUserForm) getSession().getAttribute(WebContextUtils.SES_LOGIN_USER);
		Long userid=userform.getUser().getUserId();
		UserStateInfo info=this.getStateInfo();
		List<String> depts=new ArrayList<String>();
		List<String> results=new ArrayList<String>();
		if(info==null){
			info=new UserStateInfo();
		}else{
			String dept=info.getStateBusiness();
			String items=info.getStateitem();
			String[] deptstr=dept.split(",");
			String[] itemstr=items.split(",");
			for(String str:deptstr){
				depts.add(str);
			}
			for(String str:itemstr){
				results.add(str);
			}
		}
		getRequest().setAttribute("depts", depts);
		getRequest().setAttribute("results", results);
		getRequest().setAttribute("userStateInfo", info);
		getRequest().setAttribute("userId", userid);
		return "userStateInfo";
	}
	
	/**
	 * 设置用户的自定报表
	 * @return 
	 * @throws IOException 
	 * @date 2012-4-22
	 */
	public String saveUserStateInfo() throws IOException{
		Map map=RequestUtil.getMapByRequest(getRequest());
		JSONObject jsonObject = JSONObject.fromObject(map); 
	    UserStateInfo entity=(UserStateInfo) JSONObject.toBean(jsonObject, UserStateInfo.class);
	    try {
			summaryService.saveUserStateInfo(entity);
			this.getResponse().getWriter().print("success");
		} catch (RuntimeException e) {
			this.getResponse().getWriter().print("fail");
		}
		return null;
	}
    
    /**
     * 导出统计数据
     * @return
     * @throws IOException
     */
    public String  exportStateData() throws IOException{
    	Map map=RequestUtil.getMapByRequest(getRequest());
    	String contextpath=getRequest().getRealPath("");
    	UserStateInfo info=this.getStateInfo();
    	map.put("UserStateInfo", info);
		map.put("contextpath", contextpath);
    	String path= this.summaryService.exportStateExcel(map);
		if(StringUtils.isNotBlank(path)){
			getResponse().getWriter().print(path);
		}
    	return null;
    }
   
    
    /**
     * 行政许可审批统计
     * @return
     */
    public String getResultState(){
    	return "resultState";
    }
    
    /**
     * 行政许可审批统计
     * @return
     */
    public String getAllSummaryData(){
    	Map map=RequestUtil.getMapByRequest(getRequest());
    	UserStateInfo info=this.getStateInfo();
    	map.put("UserStateInfo", info);
    	SummaryStateForm form=summaryService.searchInfo(map);
    	
    	this.getRequest().setAttribute("form",form);
    	if(info!=null){
          getRequest().setAttribute("UserStateInfo",info);
          return "userInfo";
    	}
    	return "resultInfo";
    }
    
    /**
     * 行政许可审批统计
     * @return
     */
    public String getSummaryData(){
    	Map map=RequestUtil.getMapByRequest(getRequest());
    	UserStateInfo info=this.getStateInfo();
    	map.put("UserStateInfo", info);
    	//SummaryStateForm form=summaryService.searchInfo(map);
    	Map<String,Integer> countMap=summaryService.getCount(map);
    	Map data=summaryService.findData(map);
    	getRequest().setAttribute("agreecount",countMap.get("agreecount"));
    	getRequest().setAttribute("notagreecount",countMap.get("notagreecount"));
    	getRequest().setAttribute("overtimecount",countMap.get("overtimecount"));
    	getRequest().setAttribute("gzcount",countMap.get("gzcount"));
    	getRequest().setAttribute("ontimecount",countMap.get("ontimecount"));
    	getRequest().setAttribute("othercount",countMap.get("othercount"));
    	getRequest().setAttribute("yqcount",countMap.get("yqcount"));
    	getRequest().setAttribute("data",data);
    	//getRequest().setAttribute("form",form);
    	if(info!=null){
          getRequest().setAttribute("UserStateInfo",info);
          return "userInfo";
    	}
    	return "resultInfo";
    }
    
    /**
     * 各区行政许可审批统计
     * @return
     */
    public String getAreaData(){
    	Map map=RequestUtil.getMapByRequest(getRequest());
    	UserStateInfo info=this.getStateInfo();
    	map.put("UserStateInfo", info);
    	Map<String,Integer> countMap=summaryService.getCount(map);
    	Map data=summaryService.findAreaData(map);
    	List countdata=summaryService.getAreacountData(map);
    	if(countdata!=null && countdata.size()>0){
    		for(int i=0 ;i<countdata.size();i++){
    			Map count=(Map)countdata.get(i);
    			String num=(String) count.get("countnum")==null?"0":(String) count.get("countnum");
    			String typename=(String) count.get("template_type_name");
    			if("燃气".equals(typename)){
    				getRequest().setAttribute("rqcount",num);
    			}else if("市容".equals(typename)){
    				getRequest().setAttribute("srcount",num);
    			}else if("设施".equals(typename)){
    				getRequest().setAttribute("sscount",num);
    			}else if("环卫".equals(typename)){
    				getRequest().setAttribute("hwcount",num);
    			}else if("景观".equals(typename)){
    				getRequest().setAttribute("jgcount",num);
    			}else if("余泥".equals(typename)){
    				getRequest().setAttribute("yncount",num);
    			}
    		}
    	}
    	
    	
    	getRequest().setAttribute("data",data);
    
    	return "areaData";
    }
    
    private UserStateInfo getStateInfo(){
    	LoginUserForm userform=(LoginUserForm) getSession().getAttribute(WebContextUtils.SES_LOGIN_USER);
		Long userid=userform.getUser().getUserId();
		UserStateInfo info=summaryService.getUserStateInfo(userid);
		if(info==null){
			Long[] roleids=userform.getRoleIds();
			summaryService.saveUserStateByRole(roleids);
		}
    	return info;
    }
    /**
     * 到查询首页
     * @return
     */
    public String queryList(){
    	return Constant.QUERYlIST;
    }
    
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
    
	

	/**
	 * 到区的统计
	 * @return
	 * @throws Exception
	 */
	public String toAreaState() throws Exception {
		return "toAreaState";
	}
    
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	private static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	private static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	public Object getModel() {
		return null;
	}
	
	/**
	 * 取得HttpSession的简化方法.
	 */
	public static HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	/**
	 * 直接输出文本.
	 * @see #render(String, String, String...)
	 */
	public static void renderText(final String text, final String... headers) {
		Struts2Utils.renderText(text, headers);
	}
}
